package com.xywang.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

// 操作数据库的公共类 拥有获取数据库连接，执行查询修改语句的方法，和关闭连接释放资源
public class BaseDao {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    private static int pageSize;

    //静态代码块，类加我的时候就初始化了
    static {
        Properties properties = new Properties();
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver = properties.getProperty("Driver");
        url = properties.getProperty("url");
        username = properties.getProperty("use");
        password = properties.getProperty("pwd");
        pageSize = (int) Math.floor(Double.parseDouble((properties.getProperty("pageSize"))));

    }
    // 获取页面大小
    public static int getPageSize() {
        if(pageSize<0){
            pageSize = 0;
        }
        return pageSize;
    }

    // 获取数据库连接
    public static Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }

    // 编写查询公共方法
    public static ResultSet execute(Connection conn,PreparedStatement preparedStatement,  ResultSet resultSet,String sql, Object[] params) throws SQLException {
        preparedStatement = conn.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i + 1, params[i]);
        }
        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }

    // 增删改公共方法
    public static int execute(Connection conn,PreparedStatement preparedStatement,String sql, Object[] params) throws SQLException {
        preparedStatement = conn.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i + 1, params[i]);
        }
        // 预编译的PreparedStatement对象，执行时不用加sql
        int a = preparedStatement.executeUpdate();
        return a;
    }

    // 释放资源
    public static boolean closeResource(Connection conn, Statement stat, ResultSet result) {
        boolean flag = true;

        if (result != null) {
            try {
                result.close();
                // 回收
                result = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }
        if (stat != null) {
            try {
                stat.close();
                stat = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }
        if (conn != null) {
            try {
                conn.close();
                conn = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }
        return flag;
    }

}
